﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Core;

namespace GSLMS.WebAPP.Areas.RiskAudit.Controllers.Director.DirectorDefault
{
    public class DirectorDefaultController :AdminBaseController
    {
        protected override string navTabId
        {
            get { return "RiskAudit_Director_DirectorDefault"; }
        }

        //
        // GET: /RiskAudit/DirectorDefault/

        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 待办事项
        /// </summary>
        /// <returns></returns>
        public ActionResult Gtasks()
        {
            var list = new List<TB_APP_SHOULDDOINFO>();
            list = serviceAppAudit.GetRiskAuditShouldDoList(CurentUser.UserName);

            return View(list);
        }

        /// <summary>
        /// 已办事项
        /// </summary>
        /// <returns></returns>
        public ActionResult Htasks()
        {
            return View();
        }


        #region 风控分配

        public ActionResult GAssign(int id)
        {

            ViewData["UserEffList"] = serviceEmp.GetRiskAuditUserList(CurentUser.UserName);

            var model = serviceProInfo.GetAduAssignByID((long)id);

            ViewData["status"] = model.STATUS;

            return View(model);
        }

        [HttpPost]
        public string GAssign(FormCollection form)
        {
            string result = string.Empty;
            GSLMS.Entity.TB_ADU_ASSIGN model = new TB_ADU_ASSIGN();

            try
            {
                string flowUser = form["FlowUser"];


                model.ID = 0;
                model.PROID = Request["id"] == null ? 0 : GSLMS.Core.Helper.ToLong(Request["id"]);
                model.ASSIGNBY = CurentUser.UserName;
                model.ASSIGNTIME = DateTime.Now;
                model.FLOWUSER = flowUser;


                bool isSumbit = false;
                isSumbit = serviceAduAssign.Add(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HAssign(FormCollection form)
        {

            var searchParam = base.SearchPara<TB_APP_PROINFO>(form, new TB_APP_PROINFO());
            searchParam.RiskDealingStatus = 0;
            searchParam.RiskDealingStatus2 = 1;
            searchParam.StrSort = " id desc";
            var model = serviceProInfo.GetAduAssignList(searchParam);
            ViewData["systemID"] = EnumHelper.ToSelectList<GSLMS.Entity.Enum.ApplyEnum.RegSystemID>();
            ViewData["RowCount"] = searchParam.RowCount;
            ViewData["DepID"] = new SelectList(serviceSysOrg.GetAllList(), "id", "orgname");
            ViewData["Product"] = new SelectList(serviceSysProduct.GetBySysetmID(GSLMS.Entity.Enum.SystemEnum.SystemID.典当, -1), "id", "productname");
            return View(model);
        }

        #endregion

        #region 立项申请
        public ActionResult GApplyAudit(int id)
        {
           
            var model = serviceProInfo.GetByID((long)id);
            ViewData["FinalApprover"]=new SelectList(serviceEmp.GetUserPrivEffList("",""),"username","empname");
            ViewData["AuditInfo"] = serviceAppAudit.GetDirectorAuditInfo((long)id);
            return View(model);
        }


        public ActionResult VApplyAudit(long id,long auditID)
        {

            var model = serviceProInfo.GetByID(id);
            var directorAuditModel = serviceAppAudit.GetDirectorAuditByID(auditID);
            ViewData["directorAuditModel"] = directorAuditModel;
         
            ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "empname");
            ViewData["AuditInfo"] = serviceAppAudit.GetDirectorAuditInfo(id);
            ViewData["IsDirectorAuditRetract"] = serviceAppAudit.IsDirectorAuditRetract(id,auditID);
            return View(model);
        }

        [HttpPost]
        public string GApplyAudit(FormCollection form)
        {
            string result = string.Empty;
            var model = new GSLMS.Entity.TB_APP_DIRECTORAUDIT();

            model.ID = 0;
            model.AUDITCONTENT = Request["AuditContent"];
            model.AUDITRESULT = GSLMS.Core.Helper.ToInt(Request["hidActiondo"]);
            model.AUDITBY = CurentUser.UserName;
            model.PROID = GSLMS.Core.Helper.ToLong(Request["id"]);
            model.TOUSER = Request["toUser"];

            if (model.AUDITRESULT==1&&model.TOUSER.Trim() == "")
                model.TOUSER = "001";



            try
            {
                int num = 0;
                num = serviceAppAudit.DirectorAudit(model);
                if (num==1)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HApplyAudit(FormCollection form)
        {

            var searchParam = base.SearchPara<TB_APP_DIRECTORAUDIT>(form, new TB_APP_DIRECTORAUDIT());
            searchParam.AUDITBY = CurentUser.UserName;
            searchParam.StrSort = " id desc";
            var model = serviceAppAudit.GetDirectorAuditList(searchParam) ?? new List<TB_APP_DIRECTORAUDIT>();
            ViewData["systemID"] = EnumHelper.ToSelectList<GSLMS.Entity.Enum.ApplyEnum.RegSystemID>();
            ViewData["SearchParma"] = searchParam;
            ViewData["RowCount"] = searchParam.RowCount;
            return View(model);
        }
        #endregion

        #region 展期审批
        public ActionResult GExtAudit(int id)
        {
            var model = new TB_ADU_ASSIGN();

            return View(model);
        }

        [HttpPost]
        public string GExtAudit(FormCollection form)
        {
            string result = string.Empty;
            var model = new GSLMS.Entity.TB_APP_AUDIT();


            model.AUDITCONTENT = Request["AuditContent"];
            model.AUDITRESULT = GSLMS.Core.Helper.ToInt(Request["hidActiondo"]);
            model.AUDITBY = CurentUser.UserName;
            model.PROID = GSLMS.Core.Helper.ToLong(Request["id"]);
            model.AUDITTYPE = 2;



            try
            {
                bool isSumbit = false;
                isSumbit = serviceAppAudit.ProInfoBMAudit(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HExtAudit(FormCollection form)
        {

            var searchParam = base.SearchPara<TB_APP_AUDIT>(form, new TB_APP_AUDIT());
            searchParam.AUDITTYPE = 2;
            searchParam.AUDITBY = CurentUser.UserName;
            var model = serviceAppAudit.GetList(searchParam) ?? new List<TB_APP_AUDIT>();
            ViewData["SearchParma"] = searchParam;
            ViewData["RowCount"] = searchParam.RowCount;
            return View(model);
        }
        #endregion

        #region 贷款审议
        public ActionResult GConAudit(long id)
        {
            var model = serviceProInfo.GetByID(id);
            ViewData["ConAduFlag"] = 0;
            ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "empname");
            ViewData["AuditInfo"] = serviceAppAudit.GetRiskAuditConAduAuditInfo(id);
            return View(model);
        }

        [HttpPost]
        public string GConAudit(FormCollection form)
        {

            string result = string.Empty;
            var model = new GSLMS.Entity.TB_ADU_CONADU();

            model.ID = 0;
            model.OPINION = Request["AuditContent"];
            model.ADURESULT = GSLMS.Core.Helper.ToInt(Request["hidActiondo"]);
            model.AUDITBY = CurentUser.UserName;
            model.ARRID = 0;//存储过程中通过proid取
            model.AUDITTIME =DateTime.Now;
            model.PROID = GSLMS.Core.Helper.ToLong(Request["id"]);
           



            try
            {
                if (serviceAduConAdu.Add(model))
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";

        }

        public ActionResult HConAudit(FormCollection form)
        {

            var searchParam = base.SearchPara<TB_ADU_CONARR>(form, new TB_ADU_CONARR());
            searchParam.ConAduBy = CurentUser.UserName;
            searchParam.ConAduID = CurentUser.CurrentUserInfo.ID;

            //已审批
            searchParam.STATUS = 0;
            searchParam.STATUS2 = 1;

            var model = serviceAduConArr.GetConAduList(searchParam);
            ViewData["RowCount"] = searchParam.RowCount;
            ViewData["systemID"] = EnumHelper.ToSelectList<GSLMS.Entity.Enum.ApplyEnum.RegSystemID>();
            return View(model);
        }
        #endregion

        #region 合同审批
        public ActionResult GLoanCon(int id)
        {
            var model = new TB_ADU_ASSIGN();

            return View(model);
        }

        [HttpPost]
        public string GLoanCon(FormCollection form)
        {
            string result = string.Empty;
            var model = new GSLMS.Entity.TB_APP_AUDIT();


            model.AUDITCONTENT = Request["AuditContent"];
            model.AUDITRESULT = GSLMS.Core.Helper.ToInt(Request["hidActiondo"]);
            model.AUDITBY = CurentUser.UserName;
            model.PROID = GSLMS.Core.Helper.ToLong(Request["id"]);
            model.AUDITTYPE = 2;



            try
            {
                bool isSumbit = false;
                isSumbit = serviceAppAudit.ProInfoBMAudit(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HLoanCon(FormCollection form)
        {

            var searchParam = base.SearchPara<TB_APP_AUDIT>(form, new TB_APP_AUDIT());
            searchParam.AUDITTYPE = 2;
            searchParam.AUDITBY = CurentUser.UserName;
            var model = serviceAppAudit.GetList(searchParam) ?? new List<TB_APP_AUDIT>();
            ViewData["SearchParma"] = searchParam;
            ViewData["RowCount"] = searchParam.RowCount;
            return View(model);
        }
        #endregion

        #region 出款审批
        public ActionResult GLoanApply(int id)
        {
            var model = new TB_ADU_ASSIGN();

            return View(model);
        }

        [HttpPost]
        public string GLoanApply(FormCollection form)
        {
            string result = string.Empty;
            var model = new GSLMS.Entity.TB_APP_AUDIT();


            model.AUDITCONTENT = Request["AuditContent"];
            model.AUDITRESULT = GSLMS.Core.Helper.ToInt(Request["hidActiondo"]);
            model.AUDITBY = CurentUser.UserName;
            model.PROID = GSLMS.Core.Helper.ToLong(Request["id"]);
            model.AUDITTYPE = 2;



            try
            {
                bool isSumbit = false;
                isSumbit = serviceAppAudit.ProInfoBMAudit(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HLoanApply(FormCollection form)
        {

            var searchParam = base.SearchPara<TB_APP_AUDIT>(form, new TB_APP_AUDIT());
            searchParam.AUDITTYPE = 2;
            searchParam.AUDITBY = CurentUser.UserName;
            var model = serviceAppAudit.GetList(searchParam) ?? new List<TB_APP_AUDIT>();
            ViewData["SearchParma"] = searchParam;
            ViewData["RowCount"] = searchParam.RowCount;
            return View(model);
        }
        #endregion

    }
}
